<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Class_Assess extends AdminController {

	function __construct() {
		parent::__construct();
		$this -> sidebar = "Index";
		$this -> load ->model("Class_model");
		$this-> load ->library('ProjectService/attend_service');
		$this -> load ->model("Attend_model");
		$this -> load ->model("Attend_img_model");
		$this-> load ->library('ProjectService/class_service');
		$this -> load ->model("Admin_model");
		$this -> load -> library("ProjectService/table_service");
		$this -> load -> library("ProjectService/admin_service");
		$this -> load ->library('ProjectService/upload_img_service');
		
	}

	  //督导听课
    function attendAdd($attend_id,$teacher_id=false){

		if($teacher_id){
			$data=$this->Admin_model->get_single(array('id'=>$teacher_id));
			$data['teacher_detail']=$data;
		}



    	if($attend_id == 0){
    		   $data =$this ->attend_service->getAttendData(0);
    		   if(!isset($data['attend_record'][0]['time']) || $data['attend_record'][0]['time'] == ''){
    		   	  $data['attend_record'][0]['time'] = date('Y-m-d H:i:s',time());
    		   }

    		   if(!isset($data['attend_record'][0]['commentary']) || $data['attend_record'][0]['commentary'] == ''){
    		   	  $data['attend_record'][0]['commentary'] = '';
    		   }

    		   // if(!isset($data['attend_img'][0]['img']) || $data['attend_img'][0]['img'] == ''){
    		   // 	  $data['attend_img'][0]['img'] = '';
    		   // }

				$data['is_add']=0;
		      	$date=date('Y-m-d',time());			
				$month=date('m',time());			
				if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
					$year=date('Y',time());
					$year_last=date('Y',time())+1;
					$data['class_detail']['semester']='第一学期';	
					$data['class_detail']['academic_year']=strval($year.'-'.$year_last);			
				}else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
					$year=date('Y',time())-1;
					$year_last=date('Y',time());
					$semester='第一学期';	
					$data['class_detail']['academic_year']=strval($year.'-'.$year_last);	
					$data['class_detail']['semester']='第二学期';
				}

			$data['is_show'] =0;

    	}else{
    		   $data =$this ->attend_service->getAttendData($attend_id);
    		  $data['class_list'] = $this->getClass($data['attend']['teacher_code']);

    		  //判断课程名称是下拉框还是输入框
    		  $aa = array();
    		  foreach ( $data['class_list'] as $ka => $va) {
    		  	if($data['attend']['class_name'] != $va['name']){
    		  		$aa[$ka] = 1;
    		  	}
    		  }
    		  if(count($aa) == count($data['class_list'])){
    		  	$data['is_show'] = 1;
    		  }else{
    		  	$data['is_show'] =0;
    		  }

    		   $data['is_add']= $attend_id;
    		   if($data['attend']['type']==AttendType::School[XPHEnum::Value]){
					$data['role_index']=0;
				}else if($data['attend']['type']==AttendType::Unit[XPHEnum::Value]){
					$data['role_index']=1;
				}	

				 //pri($data);
    	}

    		//获取授课教师列表
    	$list=$this->admin_service->getAllAdmin();
        foreach ($list as $key => $value) {
            $arr[$key]['id'] = $value['id'];
            $arr[$key]['usercode'] = $value['usercode'];
            $arr[$key]['username'] = $value['username'];
            $arr[$key]['dept_name'] = $value['dept_name'];
        }

      
         $data['teacher'] = $arr;

         	// 获取开课单位
		$date=date('Y-m-d',time());			
		$month=date('m',time());
		if($month==9 || $month==10 || $month==11 || $month==12){
			$year=date('Y',time());
			$year_last=date('Y',time())+1;
			$default_academic_year=strval($year.'-'.$year_last);			
		}else if($month==1 || $month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
			$year=date('Y',time())-1;
			$year_last=date('Y',time());
			$default_academic_year=strval($year.'-'.$year_last);	
		}
		$connar['items']="course_units";
		$connar['where']=$this->sqlLikeEscape('elective_lesson_no like "%??%" and course_units is not null',array($default_academic_year));
		$connar['groupby']="course_units";
		$data['course_units']=array_column($this->Class_model->get_list_full($connar), 'course_units');

		//pri($data['course_units']);
		

        //获取教师职称    
        $titlelist = array();
        foreach ((new Title)->getAll() as $k => $v){
			$titlelist[$k]['key'] = $v[XPHEnum::Value];
			$titlelist[$k]['value'] = $v[XPHEnum::Caption];
        }
		$data['teacher_title'] =$titlelist;
         //获取教师职称    
        $teachers_attribute_list = array();
        foreach ((new TeachersAttribute)->getAll() as $k => $v){
			$teachers_attribute_list[$k]['key'] = $v[XPHEnum::Value];
			$teachers_attribute_list[$k]['value'] = $v[XPHEnum::Caption];
        }

        $data['teachers_attribute_list'] =$teachers_attribute_list;

         //获取课程类型 
  		$this -> load -> model("Select_model");
		$result=$this -> Select_model -> get_list(array('type'=>SelectType::CourseType[XPHEnum::Value],'status'=>CommonStatus::UnDeleted[XPHEnum::Value]));
		$data['class_type']=array();
		foreach ($result as $k => $v) {
			array_push($data['class_type'],trim($v['option']));
		}

		//获取听课类型
		$result1=$this -> Select_model -> get_list(array('type'=>SelectType::ListenType[XPHEnum::Value],'status'=>CommonStatus::UnDeleted[XPHEnum::Value]));
		$data['ListenType']=array();
		foreach ($result1 as $k => $v) {
			array_push($data['ListenType'],trim($v['option']));
		}

    	//督导身份
		$data['AttendType']= $this->table_service->getAttendType(getSess()['id']);
		// if($data['AttendType'][0]['key']==AttendType::Teacher[XPHEnum::Value] && count($data['AttendType'])==1){
		// 	$data['can_submit']=0;
		// }else{
		// 	$data['can_submit']=1;
		// }
		$data['can_submit']=1;
		$data['title'] = "督导听课";
        $this->load->view('admin/attendAdd',$data);
    }

 

	//保存督导听课
	function saveAttend($attend_id=0){
         
		$is_submit=$this -> Attend_model -> get_single(array('id'=>$attend_id));
		if($is_submit['status']==AttendStatus::Submit[XPHEnum::Value]){
			exit(retJson('已提交无法修改',false));
		}
		$formdata=$_POST;
		$array_arr = explode(",",$_POST['teacher_name']);

		$formdata['teacher_code'] = $array_arr[0]; 
		$formdata['teacher_name'] = $array_arr[1]; 

		$data=$this -> Class_model->get_single(array('id'=>$formdata['class_id']));
		$data1=$this -> Admin_model->get_single(array('id'=>$formdata['teacher_id']));
		$data2=$this -> Admin_model->get_single(array('id'=>getSess()['id']));
		$class_id=$data['id'];
		
		// if($formdata['sub_role']==0){
		// 	$Error['error']=3;
		// 	exit(retJson('请选择反馈身份！',false,$Error));
		// }else{
		// 	$type=$formdata['sub_role'];
		// }		
		// if($type==WebRole::SchoolSupervisor[XPHEnum::Value]){
		// 	$type=AttendType::School[XPHEnum::Value];
		// }else if($type==WebRole::UnitSupervisor[XPHEnum::Value]){
		// 	$type=AttendType::Unit[XPHEnum::Value];
		// // }
		
		// if($formdata['role']==AttendType::School[XPHEnum::Caption]){
		// 	$type=AttendType::School[XPHEnum::Value];
		// }else if($formdata['role']==AttendType::Unit[XPHEnum::Caption]){
		// 	$type=AttendType::Unit[XPHEnum::Value];
		// }else if($formdata['role']==AttendType::Outside[XPHEnum::Caption]){
		// 	$type=AttendType::Outside[XPHEnum::Value];
		// }
		$type = $formdata['role'];
		// $serial_no=$formdata['attend1'];
		$academic_year=$formdata['attend2'];
		$semester=$formdata['attend3'];
		// $class_name=$data['name'];
		// $class_type=$data['type'];
		// $teacher_name=$data1['username'];
		// $title=$data1['title'];
		// $dept_name=$data1['dept_name'];
		// $age_groups=$data1['age_groups'];
		// $teachers_attribute=$data1['teachers_attribute'];
		// $listen_name=$data2['username'];
		// $listen_address=$data['classroom'];
		// $course_units=$data['course_units'];
		$listen_class=$formdata['attend4'];
		if(isset($formdata['attend5'])){
			$advice=$formdata['attend5'];
			}else{
			$advice='';
			}
	

		$class_name=$formdata['class_name'];
		$class_type=$formdata['class_type'];
		$teacher_name=$formdata['teacher_name'];
		$teacher_code=$formdata['teacher_code'];
		$title=$formdata['teacher_title'];
		$dept_name=$formdata['dept_name'];
		$age_groups=$formdata['age_groups'];
		$teachers_attribute=$formdata['teachers_attribute'];
		$listen_name=$data2['username'];
		$listen_address=$formdata['listen_address'];
		$course_units=$formdata['course_units'];
		$class_id=$formdata['class_id'];

		$listen_type = $formdata['listen_type'];
		
		// $age_groups=(new AgeGroups)->show($age_groups);
		// $teachers_attribute=(new TeachersAttribute)->show($teachers_attribute);
		//教学水平
		$teachingLevel=$formdata['teachingLevel'];
		foreach ($teachingLevel as $k => $v) {
			if($v=='请打分' ||$v==''){
				$Error['error']=1;
				exit(retJson("请选择完整的教学水平分数",false,$Error));
			}
		}
		$teachingLevel_rank=array('0','0','0','0','0');
		$teachingLevel_count=0;		
		foreach ($teachingLevel as $k => $v) {
			$teachingLevel_count=$teachingLevel_count+$teachingLevel[$k];			
		}
		if($teachingLevel_count>=90){
			$teachingLevel_rank[0]='1';
		}else if($teachingLevel_count>=80 and $teachingLevel_count<90){
			$teachingLevel_rank[1]='1';
		}else if($teachingLevel_count>=70 and $teachingLevel_count<80){
			$teachingLevel_rank[2]='1';
		}else if($teachingLevel_count>=60 and $teachingLevel_count<70){
			$teachingLevel_rank[3]='1';
		}else if($teachingLevel_count<60){
			$teachingLevel_rank[4]='1';			
		}

		//教学效果
		$teachingEffect=$formdata['teachingEffect'];
		foreach ($teachingEffect as $k => $v) {
			if($v=='0'||$v=='' ){
				$Error['error']=2;
				exit(retJson("请选择完整的教学效果分数",false,$Error));
			}
		}
		$teachingEffect_rank=array('0','0','0','0','0');
		$teachingEffect_count=0;		
		foreach ($teachingEffect as $k => $v) {
			$teachingEffect_count=$teachingEffect_count+$teachingEffect[$k];				
		}
		if($teachingEffect_count>=90){
			$teachingEffect_rank[0]='1';
		}else if($teachingEffect_count>=80 and $teachingEffect_count<90){
			$teachingEffect_rank[1]='1';
		}else if($teachingEffect_count>=70 and $teachingEffect_count<80){
			$teachingEffect_rank[2]='1';
		}else if($teachingEffect_count>=60 and $teachingEffect_count<70){
			$teachingEffect_rank[3]='1';
		}else if($teachingEffect_count<60){
			$teachingEffect_rank[4]='1';			
		}
		// if(isset($formdata['teachingReadiness'])){
		// 	$teachingReadiness=$formdata['teachingReadiness'];
		// }else{
		// 	$teachingReadiness=array();
		// }
		// $studentsNorm=$formdata['studentsNorm'];
		// $classroom_facilities=$formdata['classroom_facilities'];
		// $classroom_environmental=$formdata['classroom_environmental'];
        // foreach ((new ClassroomFacilities)->getAll()as $k => $v) {
        // 	if($classroom_facilities==$v[XPHEnum::Value]){
        // 		$classroom_facilities_item=$v[XPHEnum::Value];
        // 	}
        // }
        // foreach ((new ClassroomEnvironmental)->getAll()as $k => $v) {
        // 	if($classroom_environmental==$v[XPHEnum::Value]){
        // 		$classroom_environmental_item=$v[XPHEnum::Value];
        // 	}
        // }
   //      $attend_record=$formdata['attend_record'];
   //      $recordcount=$formdata['recordcount'];
   //      $attend_record_value=array();
   //      $k =0;
   //      for ($i=1;$i<=$recordcount;$i++){
			// //$item = array('time'=>$attend_record[$i-1].",activity"=>$attend_record[$i].",commentary"=>$attend_record[$i+1].")";			
   //      	$item['time'] =strtotime($attend_record[$k]);
   //      	$item['activity'] =$attend_record[$k+1];
   //      	$item['commentary'] =$attend_record[$k+2];
   //      	array_push($attend_record_value, $item);
   //      	$k+=3;
   //      }

        // if(!isset($this->NOTXSS_POST['attend_img'])){
        // 	exit(retJson("请上传图片",false));
        // }
        // foreach ($this->NOTXSS_POST['attend_img'] as $k => $v) {
        // 	if($v!=null ||$v!=''){
        // 		$attend_img=$this->NOTXSS_POST['attend_img'];
        // 	}else{
        // 		exit(retJson("请上传图片",false));
        // 	}
        // }
        
        if(isset($formdata['attend_img']) && $formdata['attend_img'][UploadType::AttendImg[XPHEnum::Value]]){
        	$attend_img =  $formdata['attend_img'][UploadType::AttendImg[XPHEnum::Value]];
        	$attend_img_value=array();
        	foreach($attend_img as $ks=>$vs){
        		$attend_img_value[$ks]['img'] = $vs;
        	}

        }else{
        	$attend_img_value = array();
        } 

        if(isset($formdata['attend_record_content'])){
        	$commentary = $formdata['attend_record_content'];
        }else{
        	$commentary = '';
        }
       
 
		// $attend_img_value=array();
		// if(isset($this->NOTXSS_POST['attend_img'])){
		// 	$attend_img=$this->NOTXSS_POST['attend_img'];
		// 	for ($i=0;$i<=count($attend_img)-1;$i++){
		// 		$item['img'] = $attend_img[$i];
		// 		if (checkStringIsBase64($item['img'])) {

		// 			$checkRet = uploadImg($item['img'], 'attend_img');
		// 			if (!$checkRet['status']) {
		// 				exit(toRetJson($checkRet));
		// 			}
		// 			$data['img']=$checkRet["v"];

		// 		} else {
		// 			exit(retJson('上传的图片格式不正确！', false));
		// 		}
		//	array_push($attend_img_value, $formdata['attend_img'][UploadType::AttendImg[XPHEnum::Value]]); 
		// 	}
		// }

		$is_add=$formdata['is_add'];
		if($formdata['save']==AttendStatus::Submit[XPHEnum::Value]){
			$status=AttendStatus::Submit[XPHEnum::Value];
		}else{
			$status=AttendStatus::Save[XPHEnum::Value];
		}
		// 0415新需求教学准备开展情况可选
		// foreach ($formdata['attend_teaching_readiness_content'] as $k => $v) {
		// 	if(!isset($teachingReadiness[$k])){
		// 		$teachingReadiness[$k]='';
		// 	}
		// }
		// // 0415新需求学生常态可选
		// foreach ($formdata['attend_students_norm_item'] as $k => $v) {
		// 	if(!isset($studentsNorm[$k])){
		// 		$studentsNorm[$k]='';
		// 	}
		// }
		$param=array(
			'attend'=>array(
				'class_id'=>$class_id,
				'listen_id'=>getSess()['id'],
				'type'=>$type,
				// 'serial_no'=>$serial_no,
				'academic_year'=>$academic_year,//学年
				'semester'=>$semester,//学期
				'class_name'=>$class_name,
				'class_type'=>$class_type,
				'teacher_name'=>$teacher_name,
				'teacher_code'=>$teacher_code,
				'title'=>$title,
				'dept_name'=>$dept_name,
				'age_groups'=>$age_groups,
				'teachers_attribute'=>$teachers_attribute,
				'listen_name'=>$listen_name,
				'listen_time'=>$formdata['listen_time'],
				'listen_address'=>$listen_address,
				'course_units'=>$course_units,
				'listen_class'=>$listen_class,
				// 'classroom_facilities'=>$classroom_facilities_item,
				// 'classroom_environmental'=>$classroom_environmental_item,
				'advice'=>$advice,
				'teaching_level_score'=>$teachingLevel_count,
				'teaching_effect_score'=>$teachingEffect_count,
				'status'=>$status,
				// 0415新增总分 教学70%+学生30%
				'score'=>($teachingLevel_count * 0.7+ $teachingEffect_count*0.3),
				'listen_type'=>$listen_type
			),
			'attend_teaching_content'=>array(
				0=>array('key_index'=>$formdata['attend_teaching_index'][0],'list_id'=>$formdata['attend_teaching_list_id'][0],'content'=>$formdata['attend_teaching_content'][0],'score'=>$formdata['attend_teaching_score'][0],'get_score'=>$teachingLevel[0]),
				1=>array('key_index'=>$formdata['attend_teaching_index'][1],'list_id'=>$formdata['attend_teaching_list_id'][1],'content'=>$formdata['attend_teaching_content'][1],'score'=>$formdata['attend_teaching_score'][0],'get_score'=>$teachingLevel[1])
				// ,
				// 2=>array('key_index'=>$formdata['attend_teaching_index'][2],'list_id'=>$formdata['attend_teaching_list_id'][2],'content'=>$formdata['attend_teaching_content'][2],'score'=>$formdata['attend_teaching_score'][0],'get_score'=>$teachingLevel[2])
			),
			'attend_teaching_implementation'=>array(
				0=>array('key_index'=>$formdata['attend_teaching_implementation_index'][0],'list_id'=>$formdata['attend_teaching_implementation_list_id'][0],'content'=>$formdata['attend_teaching_implementation_content'][0],'score'=>$formdata['attend_teaching_implementation_score'][0],'get_score'=>$teachingLevel[2]),
				1=>array('key_index'=>$formdata['attend_teaching_implementation_index'][1],'list_id'=>$formdata['attend_teaching_implementation_list_id'][1],'content'=>$formdata['attend_teaching_implementation_content'][1],'score'=>$formdata['attend_teaching_implementation_score'][1],'get_score'=>$teachingLevel[3])
				// ,
				// 2=>array('key_index'=>$formdata['attend_teaching_implementation_index'][2],'list_id'=>$formdata['attend_teaching_implementation_list_id'][2],'content'=>$formdata['attend_teaching_implementation_content'][2],'score'=>$formdata['attend_teaching_implementation_score'][2],'get_score'=>$teachingLevel[5]),
				// 3=>array('key_index'=>$formdata['attend_teaching_implementation_index'][3],'list_id'=>$formdata['attend_teaching_implementation_list_id'][3],'content'=>$formdata['attend_teaching_implementation_content'][3],'score'=>$formdata['attend_teaching_implementation_score'][3],'get_score'=>$teachingLevel[6]),
				// 4=>array('key_index'=>$formdata['attend_teaching_implementation_index'][4],'list_id'=>$formdata['attend_teaching_implementation_list_id'][4],'content'=>$formdata['attend_teaching_implementation_content'][4],'score'=>$formdata['attend_teaching_implementation_score'][4],'get_score'=>$teachingLevel[7]),
				// 5=>array('key_index'=>$formdata['attend_teaching_implementation_index'][5],'list_id'=>$formdata['attend_teaching_implementation_list_id'][5],'content'=>$formdata['attend_teaching_implementation_content'][5],'score'=>$formdata['attend_teaching_implementation_score'][5],'get_score'=>$teachingLevel[8])	
			),
			'attend_teaching_level'=>array(
				0=>array('item'=>$formdata['attend_teaching_level_item'][0],'remark'=>$formdata['attend_teaching_level_remark'][0],'is_check'=>$teachingLevel_rank[0]),
				1=>array('item'=>$formdata['attend_teaching_level_item'][1],'remark'=>$formdata['attend_teaching_level_remark'][1],'is_check'=>$teachingLevel_rank[1]),
				2=>array('item'=>$formdata['attend_teaching_level_item'][2],'remark'=>$formdata['attend_teaching_level_remark'][2],'is_check'=>$teachingLevel_rank[2]),
				3=>array('item'=>$formdata['attend_teaching_level_item'][3],'remark'=>$formdata['attend_teaching_level_remark'][3],'is_check'=>$teachingLevel_rank[3]),
				4=>array('item'=>$formdata['attend_teaching_level_item'][4],'remark'=>$formdata['attend_teaching_level_remark'][4],'is_check'=>$teachingLevel_rank[4])
			),
			'attend_student_status'=>array(
				0=>array('key_index'=>$formdata['attend_student_status_index'][0],'list_id'=>$formdata['attend_student_status_list_id'][0],'content'=>$formdata['attend_student_status'][0],'score'=>$formdata['attend_student_status_score'][0],'get_score'=>$teachingEffect[0])
				// ,
				// 1=>array('key_index'=>$formdata['attend_student_status_index'][1],'list_id'=>$formdata['attend_student_status_list_id'][1],'content'=>$formdata['attend_student_status'][1],'score'=>$formdata['attend_student_status_score'][1],'get_score'=>$teachingEffect[1]),
				// 2=>array('key_index'=>$formdata['attend_student_status_index'][2],'list_id'=>$formdata['attend_student_status_list_id'][2],'content'=>$formdata['attend_student_status'][2],'score'=>$formdata['attend_student_status_score'][2],'get_score'=>$teachingEffect[2]),
				// 3=>array('key_index'=>$formdata['attend_student_status_index'][3],'list_id'=>$formdata['attend_student_status_list_id'][3],'content'=>$formdata['attend_student_status'][3],'score'=>$formdata['attend_student_status_score'][3],'get_score'=>$teachingEffect[3]),
				// 4=>array('key_index'=>$formdata['attend_student_status_index'][4],'list_id'=>$formdata['attend_student_status_list_id'][4],'content'=>$formdata['attend_student_status'][4],'score'=>$formdata['attend_student_status_score'][4],'get_score'=>$teachingEffect[4]),
				// 5=>array('key_index'=>$formdata['attend_student_status_index'][5],'list_id'=>$formdata['attend_student_status_list_id'][5],'content'=>$formdata['attend_student_status'][5],'score'=>$formdata['attend_student_status_score'][5],'get_score'=>$teachingEffect[5])
			),
			'attend_teaching_effect'=>array(
				0=>array('item'=>$formdata['attend_student_status_item'][0],'remark'=>$formdata['attend_student_status_remark'][0],'is_check'=>$teachingEffect_rank[0])
				// ,
				// 1=>array('item'=>$formdata['attend_student_status_item'][1],'remark'=>$formdata['attend_student_status_remark'][1],'is_check'=>$teachingEffect_rank[1]),
				// 2=>array('item'=>$formdata['attend_student_status_item'][2],'remark'=>$formdata['attend_student_status_remark'][2],'is_check'=>$teachingEffect_rank[2]),
				// 3=>array('item'=>$formdata['attend_student_status_item'][3],'remark'=>$formdata['attend_student_status_remark'][3],'is_check'=>$teachingEffect_rank[3]),
				// 4=>array('item'=>$formdata['attend_student_status_item'][4],'remark'=>$formdata['attend_student_status_remark'][4],'is_check'=>$teachingEffect_rank[4])
			),
			/* 'attend_teaching_readiness'=>array(
				0=>array('item'=>$formdata['attend_teaching_readiness_content'][0],'option'=>$formdata['attend_teaching_readiness_option'][0],'check_value'=>$teachingReadiness[0]),
				1=>array('item'=>$formdata['attend_teaching_readiness_content'][1],'option'=>$formdata['attend_teaching_readiness_option'][1],'check_value'=>$teachingReadiness[1]),
				2=>array('item'=>$formdata['attend_teaching_readiness_content'][2],'option'=>$formdata['attend_teaching_readiness_option'][2],'check_value'=>$teachingReadiness[2]),
				3=>array('item'=>$formdata['attend_teaching_readiness_content'][3],'option'=>$formdata['attend_teaching_readiness_option'][3],'check_value'=>$teachingReadiness[3]),
				4=>array('item'=>$formdata['attend_teaching_readiness_content'][4],'option'=>$formdata['attend_teaching_readiness_option'][4],'check_value'=>$teachingReadiness[4]),
				5=>array('item'=>$formdata['attend_teaching_readiness_content'][5],'option'=>$formdata['attend_teaching_readiness_option'][5],'check_value'=>$teachingReadiness[5]),
				6=>array('item'=>$formdata['attend_teaching_readiness_content'][6],'option'=>$formdata['attend_teaching_readiness_option'][6],'check_value'=>$teachingReadiness[6]),
				7=>array('item'=>$formdata['attend_teaching_readiness_content'][7],'option'=>$formdata['attend_teaching_readiness_option'][7],'check_value'=>$teachingReadiness[7]),
				8=>array('item'=>$formdata['attend_teaching_readiness_content'][8],'option'=>$formdata['attend_teaching_readiness_option'][8],'check_value'=>$teachingReadiness[8]),
				9=>array('item'=>$formdata['attend_teaching_readiness_content'][9],'option'=>$formdata['attend_teaching_readiness_option'][9],'check_value'=>$teachingReadiness[9]),
				10=>array('item'=>$formdata['attend_teaching_readiness_content'][10],'option'=>$formdata['attend_teaching_readiness_option'][10],'check_value'=>$teachingReadiness[10]),
				11=>array('item'=>$formdata['attend_teaching_readiness_content'][11],'option'=>$formdata['attend_teaching_readiness_option'][11],'check_value'=>$teachingReadiness[11])
			),
			'attend_students_norm'=>array(
				0=>array('item'=>$formdata['attend_students_norm_item'][0],'check_value'=>$studentsNorm[0]),
				1=>array('item'=>$formdata['attend_students_norm_item'][1],'check_value'=>$studentsNorm[1]),
				2=>array('item'=>$formdata['attend_students_norm_item'][2],'check_value'=>$studentsNorm[2]),
				3=>array('item'=>$formdata['attend_students_norm_item'][3],'check_value'=>round($studentsNorm[3],2).'%'),
				4=>array('item'=>$formdata['attend_students_norm_item'][4],'check_value'=>$studentsNorm[4]),
				5=>array('item'=>$formdata['attend_students_norm_item'][5],'check_value'=>$studentsNorm[5]),
				6=>array('item'=>$formdata['attend_students_norm_item'][6],'check_value'=>$studentsNorm[6]),
				7=>array('item'=>$formdata['attend_students_norm_item'][7],'check_value'=>$studentsNorm[7]),
				8=>array('item'=>$formdata['attend_students_norm_item'][8],'check_value'=>$studentsNorm[8])				
			),
			'attend_classroom_facilities'=>array('option'=>$formdata['attend_classroom_facilities_option'],'check_value'=>$formdata['classroom_facilities']),
			'attend_classroom_environmental'=>array('option'=>$formdata['classroom_environmental_option'],'check_value'=>$formdata['classroom_environmental']), */
			'attend_record'=> array(
				0=>array('time'=>$formdata['attend_record_time'],'activity'=>null,'commentary'=>$commentary)
			),
			'attend_img'=>$attend_img_value
		);
		
		
		if($is_add==0){
			$result=$this->attend_service->saveAttend($param);
		}else{
			$result=$this->attend_service->saveAttend($param,$is_add);//1.id,2,
		}
		unset($_SESSION[SESS_ASSESS]);
		if($formdata['save']==AttendStatus::Submit[XPHEnum::Value]){
			$this -> Attend_model -> update(array('id'=>$attend_id,'back_time'=>null));//清空回退时间
			exit(retJson('提交成功！', true,$result));
		}else{
			exit(retJson('保存成功！', true,$result));
		}
		
	}

	 //督导批量提交听课
    function moreSubmit(){

        if(!isset($_POST['id'])){
            exit(retJson('参数错误',false));
        }


        $ids =  implode(',',$_POST['id']);

        //听课 
        $connar['where'] = "id in(".$ids.")";
        $attendArr = $this -> Attend_model -> get_list_full($connar);

        foreach ($attendArr as $key => $value) {
            // if($value['status'] == AttendStatus::Submit[XPHEnum::Value]){
            //     exit(retJson('听课教师:'.$value['listen_name'].'---授课教师:'.$value['teacher_name'].'已提交无法修改',false));
            // }else{
            //     $data['id'] = $value['id'];
            //     $data['status'] =  AttendStatus::Submit[XPHEnum::Value];
            //     $data['back_time'] =  null;
            //     $this -> Attend_model ->update($data,'id');
            // }
            // 批量提交未提交的听课记录
            if($value['status'] == AttendStatus::Save[XPHEnum::Value]){
            	$data['id'] = $value['id'];
                $data['status'] =  AttendStatus::Submit[XPHEnum::Value];
                $data['back_time'] =  null;
                $this -> Attend_model ->update($data,'id');
            }
        }

        exit(retJson("提交成功.", true));

    }


	//已提交督导听课
	function SubmittedAssess(){
		$connar['where']=$this->sqlEscape("listen_id=?? and status<>??",array($_SESSION[SESS_USER_HOME]['id'],AttendStatus::Deleted[XPHEnum::Value]));
		$connar['orderby']='created desc';
		$result['detail'] = $this -> Attend_model ->get_list_full($connar);
		foreach ($result['detail'] as $k => $v) {
			$result['detail'][$k]['type_char']=(new AttendType)->show($v['type']);
		}
		// var_dump($result);
		$this-> load ->view("home/submitted_assess",$result);
	}


	function base64EncodeImage ($image_file) {
		$base64_image = '';
		$image_info = getimagesize($image_file);
		$image_data = fread(fopen($image_file, 'r'), filesize($image_file));
		$base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
		return $base64_image;
	}


 // 0426教师信息搜索
    function getTeatcherList(){
        $this->load->model('Admin_model');
        $connar['where']=$this->sqlLikeEscape('status=?? and (username like "%??%" or usercode like "%??%" ) and role_id=??',array(CommonStatus::UnDeleted[XPHEnum::Value],$_POST['teacher_name'],$_POST['teacher_code'],WebRole::Teacher[XPHEnum::Value]));
        $data=$this->Admin_model->get_single_full($connar);

        $data['title'] = (new Title)->show($data['title']);
         $data['teachers_attribute'] = (new TeachersAttribute)->show($data['teachers_attribute']);

       // pri($data);
        exit(retJson('返回搜索结果',true,$data)) ; 
    }


       // 0507课程信息搜索
    function getClassList(){
    	$teacher_code=$_POST['teacher_code'];

    	$data = $this->getClass($teacher_code);
      

       // pri($data);
       
        exit(retJson('返回搜索结果',true,$data)) ; 
    }


    function getClass($teacher_code){
  		$this->load->model("Class_model");
        $month=date('m',time());
        if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
            $year=date('Y',time());
            $year_last=date('Y',time())+1;
            $default_academic_year=strval($year.'-'.$year_last);            
        }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
            $year=date('Y',time())-1;
            $year_last=date('Y',time());
            $default_academic_year=strval($year.'-'.$year_last);    
        }
         
 		$connar['where'] = 'db_class.elective_lesson_no like "%'.$default_academic_year.'%"';

        if(isset($teacher_code)){
           $connar['where'] .= 'and db_class.teacher_id like "%'.$teacher_code.'%"';
        }


        $connar['join']=array('db_admin','db_admin.usercode=db_class.teacher_id');
        $connar['items']="name,db_class.id,username,db_admin.dept_name";
        $connar['groupby']="name";
        $data=$this->Class_model->get_list_full($connar);
        foreach ($data as $k => $v) {
            if(!$v['username']){
                $data[$k]['username']='';
            }
            if(!$v['dept_name']){
                $data[$k]['dept_name']='';
            }else{
                $data[$k]['dept_name']='--'.$v['dept_name'];
            }
        }


        return  $data;


    }


	function checkClass(){
		$date=date('Y-m-d',time());			
		$month=date('m',time());
		if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
			$year=date('Y',time());
			$year_last=date('Y',time())+1;
			$default_academic_year=strval($year.'-'.$year_last);			
		}else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
			$year=date('Y',time())-1;
			$year_last=date('Y',time());
			$default_academic_year=strval($year.'-'.$year_last);	
		}
		// 获取教师
		$connar['where']=$this->sqlLikeEscape('elective_lesson_no like "%??%" and name="??" and teacher_id like "%??%" and classname="??" ',array($default_academic_year,$_POST['name'],$_POST['teacher_id'],$_POST['classname']));
		$data=$this->Class_model->get_single_full($connar);
		if($data){
			exit(retJson("查询成功",true,$data));
		}else{
			exit(retJson("不存在该课程",false));
		}
	}


	 // 0507 课程信息session保存
    function setClass(){
         $this->load->model("Class_model");
        if(isset($_POST['class_name'])){

            if(isset($_POST['teacher_code'])){
                $month=date('m',time());
                if($month==9 || $month==10 || $month==11 || $month==12 || $month==1){
                    $year=date('Y',time());
                    $year_last=date('Y',time())+1;
                    $default_academic_year=strval($year.'-'.$year_last);            
                }else if($month==2 || $month==3 || $month==4 || $month==5 || $month==6 || $month==7 || $month==8){
                    $year=date('Y',time())-1;
                    $year_last=date('Y',time());
                    $default_academic_year=strval($year.'-'.$year_last);    
                }
                $connar['where']=$this->sqlLikeEscape('name like "%??%" and elective_lesson_no like "%??%" and teacher_id like "%??%"',array($_POST['class_name'],$default_academic_year,$_POST['teacher_code']));
                $data=$this->Class_model->get_single_full($connar);

                if($data){
                     $arraylist = array();
                     $arraylist['class_id'] = $data['id'];
	                 $arraylist['classtype'] = $data['type'];
	                 $arraylist['classname'] = $data['classname'];
	                 $arraylist['course_units'] = $data['course_units'];
	                if($data['classroom']){
	                	 $arraylist['classroom'] = explode(';',$data['classroom'])[0];               	
	                }
                    
                }

            }
            exit(retJson('返回数据',true, $arraylist)); 
        }else{
        	exit(retJson('系统错误',false)); 
        }
        
    }

}